<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>角色管理页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/resources/favicon.ico">
    <link rel="stylesheet" href="resources/layui/css/layui.css" th:href="@{/resources/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" href="resources/css/public.css" th:href="@{/resources/css/public.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/layui_ext/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/layui_ext/dtree/font/dtreefont.css}" media="all"/>
</head>
<body class="childrenBody">
<!--查询条件-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 15px;">
    <legend>查询条件</legend>
</fieldset>
<form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" class="layui-input input-radius"
                       placeholder="请输入角色名称">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">角色备注</label>
            <div class="layui-input-inline">
                <input type="text" name="remark" id="remark" class="layui-input input-radius" placeholder="请输入角色备注">
            </div>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">是否可用</label>
        <div class="layui-input-inline">
            <input type="radio" name="available" value="1" title="可用">
            <input type="radio" name="available" value="0" title="不可用">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block" style="text-align: center;padding-right: 15%;">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" lay-submit="" lay-filter="doSearch"><i
                    class="layui-icon layui-icon-search layui-icon-normal"></i>查询
            </button>
            <button type="reset" id="resetRole" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-warm"><i
                    class="layui-icon layui-icon-refresh"></i><span>重置</span>
            </button>
        </div>
    </div>
</form>

<!--数据表格-->
<div>
    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
    <div id="roleToolBar" style="display: none">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
    </div>
    <div id="roleRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm layui-btn-radius"><i
                class="layui-icon layui-icon-edit"></i>编辑
        </button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</div>

<!--添加和修改弹出层-->
<div id="addOrUpdateDiv" style="display: none;padding: 10px;padding-right: 5%">
    <form action="" method="post" class="layui-form" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-block">
                    <input type="hidden" name="roleId">
                    <input type="text" name="name" lay-verify="required" autocomplete="off"
                           class="layui-input input-radius" placeholder="请输入角色名称">
                </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色备注</label>
            <div class="layui-input-block">
                <input type="text" name="remark" lay-verify="required" autocomplete="off"
                       class="layui-input input-radius" placeholder="请输入角色名称">
            </div>
        </div>
        <div class="layui-form-item">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-block">
                    <input type="radio" name="available" value="1" title="可用">
                    <input type="radio" name="available" value="0" title="不可用" checked="">
                </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center">
                <button type="button" class="layui-btn layui-btn-radius" lay-submit="" lay-filter="doSubmit" id="doSubmit"><i
                        class="layui-icon layui-icon-search layui-icon-normal"></i>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm"><i
                        class="layui-icon layui-icon-refresh"></i><span>重置</span>
                </button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/resources/layui/layui.js"></script>

<script type="text/javascript">
    layui.use(['jquery','form','table','layer'],function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        //加载数据
        var tableIns = table.render({
            elem: "#roleTable",
            url: "/role/loadAllRole",
            toolbar: "#roleToolBar",
            title: "角色数据表格",
            height: "full-220",
            page: true,
            cols: [ [
                {field: 'roleId', title: 'ID', align: 'center',sort: true},
                {field: 'name', title: '角色名称', align: 'center'},
                {field: 'remark', title: '角色备注', align: 'center'},
                {field: 'available', title: '是否可用', align: 'center',templet:function (d) {
                    return d.available==1?'<font color="blue">可用</font>':'<font color="red">不可用</font>';
                }},
                {field: 'createtime', title: '创建时间', align: 'center'},
                {fixed: 'right', title: '操作', toolbar: '#roleRowBar', align: 'center',width:280}
            ] ],
            done:function (data, curr, count) {
                //不是第一页时，如果当前返回的数据为0那么就返回上一页
                if(data.data.length==0&&curr!=1){
                    tableIns.reload({
                        page:{
                            curr:curr-1
                        }
                    })
                }
            }
        });

        //点击重置按钮时，重置表单的值
        $("#resetRole").click(function () {
            $("#searchFrm")[0].reset();
        });

        //监控模糊查询按钮事件
        form.on("submit(doSearch)", function (data) {
            var params = $("#searchFrm").serialize();
            tableIns.reload({
//                where: data.field,
                url:"/role/loadAllRole?"+params,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监控工具条事件
        table.on("toolbar(roleTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddLayer();
                break;
            };
        });

        //监控行工具条事件
        table.on("tool(roleTable)",function (obj) {
            //获取当前行数据
            var data = obj.data;
            obj.data.roleId
            switch (obj.event){
                case 'update':
                    updateRole(data);
                    break;
                case 'delete':
                    deleteRole(data);
                break;
            };
        });

        var mainIndex;
        var url;

        //打开添加弹出层
        function openAddLayer() {
            mainIndex = layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','350px'],
                title:'添加角色',
                success:function () {
                    $("#dataFrm")[0].reset();
                    url="/role/addRole";
                }
            });
        }

        //打开修改弹出层
        function updateRole(data) {
            mainIndex = layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','350px'],
                title:'修改角色',
                success:function () {
                    //清空原有的数据
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url="/role/updateRole";
                }
            });
        }

        //提交按钮事件
        form.on("submit(doSubmit)",function (data) {
            $.post(url,data.field,function (res) {
                if (res.code==200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
            return false;
        });

        //删除
        function deleteRole(data) {
            layer.confirm('你确定要删除【'+data.name+'】这条数据吗？如果您删除该角色，那么所有拥有该角色的用户将没有角色，请重新给这些用户分配角色！',{icon: 3,title:'提示'},function (index) {
                $.post("/role/deleteRole",{id:data.roleId},function (res) {
                    if (res.code==200){
                        tableIns.reload({
                            where:"",
                        });
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }
    });

</script>

</body>
</html>